40923214 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • hoomwork(1)
    • PCH5 主板、擴展總線和處理器
      • 主板和組件
      • 擴展插槽
      • 芯片組
      • 連結器
      • 處理器歷史
      • 虛擬化
      • 集成處裡單元(GPU)
      • CPU散熱
    • CPH6 內存與BIOS
      • RAM基礎
      • 內存模塊
      • RAM歷史
      • 奇偶校驗
      • 安裝內存模塊
      • BIOS、CMOS簡介
      • 配置系統BIOS
      • 開機自檢(POST)和錯誤報告
      • BIOS更新
  • homework(2)
    • homework(2-1)
  • homework(3)
    • PRACTICE PYTHON
    • Guess Letters
  • 心得
  • 開發
homework(3) << Previous Next >> Guess Letters

PRACTICE PYTHON

Character Input 

input strings types int

Calibrating the exercises to the audience is going to be a challenging task, so I ask you to bear with me if the exercises are too easy or too hard. Every week there will be a poll you can click on to discuss whether the exercise is too easy or too hard and hopefully in a few weeks, I’ll get the level right. Let’s get to it! I will start with the exercise and include a discussion later, in case you want the extra challenge.

校準練習是一項艱難的任務,因此,我請您忍受練習是否太容易或太難。每週都會有一個小測驗,您可以練習以討論是太容易還是太難,希望在幾週後,我會把水平調到正確的水平。讓我們開始吧!如果您需要額外的挑戰,我將從練習開始,並在以後進行討論。

Exercise 1 (and Solution)

Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.

Extras:

  1. Add on to the previous program by asking the user for another number and printing out that many copies of the previous message. (Hint: order of operations exists in Python)
  2. Print out that many copies of the previous message on separate lines. (Hint: the string "\n is the same as pressing the ENTER button)

創建一個程序,要求用戶輸入他們的姓名和年齡。打印給他們的消息,告訴他們他們將滿100歲的年份。

附加功能:

  1. 通過詢問用戶另一個號碼並打印出先前消息的許多副本來添加到先前的程序中。(提示:操作順序在Python中存在)
  2. 在單獨的行上打印出先前消息的許多副本。(提示:字符串"\n與按ENTER按鈕相同)

Discussion

Concepts for this week:

  • Getting user input
  • Manipulating strings (a few ways)

User input in Python

To get user input in Python (3), the command you use is input(). Store the result in a variable, and use it to your heart’s content. Remember that the result you get from the user will be a string, even if they enter a number.

要在Python(3)中獲得用戶輸入,您使用的命令是input()。將結果存儲在變量中,並將其用於您的內心。請記住,即使用戶輸入數字,您從用戶那裡得到的結果也將是字符串。

For example,

name = input("Give me your name: ")
print("Your name is " + name)

What this will print in the terminal (or the shell, whatever you are running Python in) will be:

>>> Give me your name: Michele
Your name is Michele

What happens at the end of input() is that it waits for the user to type something and press ENTER. Only after the user presses ENTER does the program continue.

Manipulating strings (a few ways)

What you get from the input() function is a string. What can you do with it?

First: Make the string into a number. Let’s say you are 100% positive that the user entered a number. You can turn the string into an integer with the function int(). (In a later exercise or two or three there will be questions about what to do when the user does NOT enter a number and you try to do this; for now don’t worry about that problem). Here is what this looks like:

您從input()函數中得到的是一個字符串。你能做什麼呢?

第一:將字符串變成數字。假設您100%肯定用戶輸入了數字。您可以使用函數將字符串轉換為整數int()。(在以後的一兩三個練習中,當用戶不輸入數字而您嘗試這樣做時,將會出現有關如何處理的問題;現在不必擔心該問題)。看起來是這樣的:

age = input("Enter your age: ")
age = int(age)

(or, if you want to be more compact with your code)

age = int(input("Enter your age: "))

In both cases, age will hold a variable that is an integer, and now you can do math with it.

(Note, you can also turn integers into strings exactly in the opposite way, using the str() function)

Second: Do math with strings. What do I mean by that? I mean, if I want to combine (concatenate is the computer science word for this) strings, all I need to do is add them:

	
print("Were" + "wolf")
print("Door" + "man")
print("4" + "chan")
print(str(4) + "chan")

The same works for multiplication:

print(4 * "test")

but division and subtraction do not work like this. In terms of multiplication, the idea of multiplyling two strings together is not well-defined. What does it mean to multiply two strings in the first place? However, it makes sense in a way to specify multiplying a string by a number - just repeat that string that number of times. Try this in your own program with all the arithmetic operations with numbers and strings - the best way to get a feel for what works and what doesn’t is to try it!

但是除法和減法不是這樣的。就乘法而言,將兩個字符串相乘的想法尚不明確。首先將兩個字符串相乘是什麼意思?但是,以某種方式指定將字符串乘以數字是有意義的-只需將該字符串重複該次數即可。在您自己的程序中嘗試使用帶有數字和字符串的所有算術運算-嘗試一下什麼有效和什麼無效的最佳方法是嘗試一下!


homework(3) << Previous Next >> Guess Letters

Copyright © All rights reserved | This template is made with by Colorlib